#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "errsys.h"

int main(int argc, char **argv)
{
	if(argc!=2)
	{
		fprintf( stderr,"Usage:%s file\n",argv[0] );
		exit(1);
	}

	int fds[2];
	pid_t pid;
	
	if( pipe(fds)==-1 )
		err_sys("pipe");

	pid=fork();
	if(pid==-1)
		err_sys("fork");

	if(pid==0) //child
	{
		close(fds[0]);
		int fd=open(argv[1],O_RDONLY);
		char buf[100];
		ssize_t sz;
		while( (sz=read(fd,buf,100))>0 )
			if(write(fds[1],buf,sz)!=sz)
				err_sys("write");

		if(sz<0)
			err_sys("read");

		exit(0);
	}else{
		close(fds[1]);
		char buf2[100];
		ssize_t sz2;
		while( (sz2=read(fds[0],buf2,100))>0 )
			if(write(STDOUT_FILENO,buf2,sz2)!=sz2)
				err_sys("write");

		if(sz2<0)
			err_sys("read");

		exit(0);
	}

	return 0;
}

